import java.util.Scanner;


public class SecureNotepad extends SimpleNotepad {
	//Constants
	private String password;

	
	//Constructors
	private SecureNotepad(int numberOfPages, String enteredPassword) {
		super(numberOfPages);
		this.setPassword(enteredPassword);
	}


	//Methods
	
	public static SecureNotepad createSecureNotepad (int numberOfPages, String enteredPassword){
		if(validatePassword(enteredPassword)){
			return new SecureNotepad(numberOfPages, enteredPassword);
		}
		return null;
	}
	
	public Page getPage(int pageNumber,String enteredPassword){
		if(checkPassword("get page")){
			 return super.getPage(pageNumber);
		}
		return null;
	}
	
	private void setPassword(String password){
		this.password = password;
	}
	private static  boolean validatePassword(String password){
		if(password != null && password.length() >=5){
			boolean hasUpper = false;
			boolean hasLower = false;
			boolean hasDigit = false;
			for (int i = 0; i < password.length(); i++) {
				if(Character.isUpperCase(password.charAt(i))){
					hasUpper = true;
				}
				if(Character.isLowerCase(password.charAt(i))){
					hasLower = true;
				}
				if(Character.isDigit(password.charAt(i))){
					hasDigit = true;
				}
			}
			if(hasUpper && hasLower && hasDigit ){
				return true;
			}else{
				System.out.println("The Password must have  at least one Uppercase letter, one LowerCase letter, and a Digit");
			}
		}else{
			System.out.println("Password length must be atleast 5 symbols");
		}
		return false;
	}
	
	@Override
	public void addTextToPage(int pageNumber, String text) {
		if(checkPassword("add text on page")){
			super.addTextToPage(pageNumber, text);
		}
		
	}

	@Override
	public void overWriteTextOnPage(int pageNumber, String text) {
		if(checkPassword("Overwrite text on page")){
			super.overWriteTextOnPage(pageNumber, text);
		}
		
	}

	@Override
	public void deleteTextOnPage(int pageNumber) {
		if(checkPassword("Delete text on page")){
			super.deleteTextOnPage(pageNumber);
		}
		
	}

	@Override
	public void showNotepad() {
		if(checkPassword("Show notepad")){
			super.showNotepad();
		}
		
	}
	
	boolean checkPassword(String operation){
		if (operation != null) {
			Scanner sc = new Scanner(System.in);
			System.out.println("Enter Password to use operation "+operation+" : ");
			String enteredPassword = sc.nextLine();
			if (this.password.equals(enteredPassword)) {
				return true;
			}
			System.out.println("Wrong Password!Operation "+ operation + " cannot be applied");
		}
		return false;
	}
}
